package com.jinzhi.wxpay.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.utils.Result;
import com.jinzhi.oss.service.OssService;
import com.jinzhi.wxpay.entity.OrderInfo;
import com.jinzhi.wxpay.entity.ProductDO;
import com.jinzhi.wxpay.entity.ProductVO;
import com.jinzhi.wxpay.service.ProductService;
import com.jinzhi.wxpay.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@CrossOrigin //开放前端的跨域访问
@Api(tags = "商品管理")
@Controller
@RequestMapping("/api/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @Autowired
    private OssService ossService;


    @ApiOperation("测试接口")
    @GetMapping("/test")
    public R test(){

        return R.ok().data("message", "hello").data("now", new Date());
    }

    @ApiOperation("跳转到商品详情页面")
    @GetMapping("/info")
    public String productInfo(){
        return  "/jzweb/productInfo/productinfo";
    }


    @ApiOperation("跳转到商品更改页面")
    @GetMapping("/edit")
    public  String productEdit(){
        return "/jzweb/productInfo/edit";
    }



    @ApiOperation("商品列表")
    @ResponseBody
    @PostMapping("/list")
    public Result<?> list(int current, int number,ProductVO productVO){
        //条件查询出商品信息列表
        Page<ProductVO> page = productService.listProductByDesc(Integer.valueOf(current), Integer.valueOf(number), productVO);
        return Result.ok(page);
    }



    @ResponseBody
    @PostMapping("/upload")
    Result<String> upload(@RequestParam("file") MultipartFile file) {
        String url  =ossService.uploadFileOss(file);
        return Result.ok(url);
    }


//    @ApiOperation("商品详细信息")
//    @ResponseBody
//    @PostMapping("/getProductEdit")
//    public  Result<?>  getProductEdit(){
//
//
//    }

    @ApiOperation("添加商品信息")
    @ResponseBody
    @PostMapping("/insertProduct")
    public  Result<String> insertProduct(ProductDO productDO){
        productService.insert(productDO);
        return Result.ok();
    }


    @ApiOperation("更改商品信息")
    @ResponseBody
    @PostMapping("/getProductEdit")
    public Result<String> getProductEdit(ProductDO productDO){
        EntityWrapper<ProductDO> Wrapper = new EntityWrapper<>();
        Wrapper.eq("id",productDO.getId());
        productService.update(productDO,Wrapper);
        return Result.ok();
    }


    @ApiOperation("删除商品信息")
    @ResponseBody
    @PostMapping("/deleteProduct")
    public Result<String> deleteProduct(Long id){//传入商品id
        EntityWrapper<ProductDO> Wrapper = new EntityWrapper<>();
        Wrapper.eq("id",id);
        productService.delete(Wrapper);
        return Result.ok();
    }


    @ApiOperation("商品上下架-更改商品的上下架状态，下架后用户看不见")
    @ResponseBody
    @PostMapping("/getGoodsOffTheShelf")
    public  Result<String>  goodsOffTheShelf(ProductDO productDO){//传入id，goodsOffTheShelfStatus（上下架状态）
        EntityWrapper<ProductDO> Wrapper = new EntityWrapper<>();
        Wrapper.eq("id",productDO.getId());
        productService.update(productDO,Wrapper);
        return  Result.ok();
    }


}
